package com.qfedu.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qfedu.entity.Anonymity;
import com.qfedu.entity.ResponseData;
import com.qfedu.entity.User;
import com.qfedu.service.AnonymityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 用于处理匿名投诉的controller
 */
@RestController
@RequestMapping("/anonymity")
public class AnonymityController {

    @Autowired
    private AnonymityService anonymityService;


    @RequestMapping("/queryAnonymity.do")
    public ResponseData queryAnonymity(int page,int limit) {
        ResponseData<Object> responseData = new ResponseData<>();

        // 设置分页查询
        PageHelper.startPage(page,limit);

        // 查询到所有的匿名投诉信息
        List<Anonymity> anonymityList = anonymityService.findAllAnonymity();

        PageInfo<Anonymity> pageInfo = new PageInfo<>(anonymityList);
        int total = (int) pageInfo.getTotal();

        responseData.setCode(0);
        responseData.setMsg("匿名投诉信息");
        responseData.setCount(total);
        responseData.setData(anonymityList);

        return responseData;
    }

    @RequestMapping("/add.do")
    public ResponseData add(Anonymity anonymity,HttpServletRequest request) {

        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");

        String no = user.getNo();

        anonymity.setU_no(no);

        // 调用service进行添加
        int add = anonymityService.add(anonymity);

        ResponseData<Object> responseData = new ResponseData<>();
        if (add > 0) {
            responseData.setCode(0);
            responseData.setMsg("投诉成功");
        } else {
            responseData.setCode(1);
            responseData.setMsg("系统异常");
        }

        return responseData;
    }
}
